﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WMSWebApi.Interface;
using WMSWebApi.Models;

namespace WMSWebApi.Implement
{
    public class ImpTask : ITask
    {
        WMSDATAContext _context = new WMSDATAContext();
        TasksTable tasks;
        public int Add(TasksTable item)
        {
            tasks = new TasksTable
            {
                TaskNumber = item.TaskNumber,
                TaskType = item.TaskType,
                TaskStatus = item.TaskStatus,
                MaterialName = item.MaterialName,
                MaterialNumber = item.MaterialNumber,
                RiseSpot = item.RiseSpot,
                EndSpot = item.EndSpot,
                StartTime = item.StartTime,
                EndTime = item.EndTime
            };
            _context.TasksTables.Add(item);//提交到内存
            return _context.SaveChanges(); //提交到数据库
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            TasksTable tasks = _context.TasksTables.Find(id);
            _context.TasksTables.Remove(tasks);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 根据用户名获取任务
        /// </summary>
        /// <param name="stationName"></param>
        /// <returns></returns>
        public IEnumerable<TasksTable> Get(string stationName,string stationType)
        {
            StationsTable stations = _context.StationsTables.First(s=>s.StationName== stationName);
            return _context.TasksTables.Where(t => t.RiseSpot == stations.StationNumber && t.TaskType==stationType).ToList<TasksTable>();
        }  
        /// <summary>
        /// 查询多有任务
        /// </summary>
        /// <returns></returns>
        public IEnumerable<TasksTable> GetAll()
        {
            return _context.TasksTables.ToList<TasksTable>();
        }
        /// <summary>
        /// 修改任务
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(TasksTable item)
        {
            tasks = _context.TasksTables.Find(item.Id);
            tasks.TaskNumber = item.TaskNumber;
            tasks.TaskStatus = item.TaskStatus;
            tasks.TaskType = item.TaskType;
            tasks.MaterialName = item.MaterialName;
            tasks.MaterialNumber = item.MaterialNumber;
            tasks.RiseSpot = item.RiseSpot;
            tasks.EndSpot = item.EndSpot;
            tasks.StartTime = item.StartTime;
            tasks.EndTime = item.EndTime;
            _context.TasksTables.Update(tasks);
            return _context.SaveChanges();
        }
        /// <summary>
        /// 根据工位名称，任务类型查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        List<TasksTable> ITask.Get(string name, string type)
        {
            throw new NotImplementedException();
        }
    }
}
